﻿#ifndef FACEWGT_H
#define FACEWGT_H

#include <opencv2/opencv.hpp>
#include <opencv2/video.hpp>
#include <opencv2/videoio.hpp>
#include <QMutex>
#include <QPixmap>
#include <QWidget>

#include <thread>

#include "FaceModule/FaceModule.h"


namespace Ui {
class FaceWgt;
}

class FaceWgt : public QWidget
{
    Q_OBJECT

public:
    explicit FaceWgt(QWidget *parent = nullptr);
    ~FaceWgt();

    QPixmap getCurImage();

    void switchAutoVirtfy(bool flag); //切换识别模式和注册模式.
    void forceStop();
    void onRemoveFace(uint16_t faceId);

signals:
    void sigPlayViocePrompt(QString text); //通知上层播放提示声.
    void sigFindFace(); //通知模块检索人脸，或者模块识别人脸.
    void sigUserLoginOver(bool flag, int faceId, QString info); //用户注册结束.
    void sigUserSign(int faceId); //用户签到.
    void sigUpdateImage(); //通知上层更新图片.

public slots:
    void onGetStatus(uint8_t status, QString errInfo, bool flag);
    void onVerify(uint16_t faceId, QString errInfo, bool flag);
    void onEnroll(uint16_t faceId, QString errInfo, bool flag);
    void onDeleteAllUser(QString errInfo, bool flag);
    void onGetAllUserId(QByteArray useId, uint8_t userCnt, QString errInfo, bool flag);

private:
    void thCaptureImage(); //捕获摄像头图像.
    QImage cvMat2Image(const cv::Mat mat);
    void setupSignals();

private:
    Ui::FaceWgt *ui;

    FaceModule *m_face {nullptr};
    bool m_verifiing {false}; //解锁标记，用于避免重复解锁.
    bool m_enrolling {false}; //注册标记，用于控制避免重复发起注册.
    bool m_autoVerify {true}; //自动人脸验证.
    bool m_capture {false}; //捕获图像的标志位.
    std::thread m_th; //
    QMutex m_mutex; //bool m_dataUsed {false};
    QPixmap m_pix;
};

#endif // FACEWGT_H
